﻿using System.Collections.Generic;
using Acaweb.Data.Contracts;
using Acaweb.Data.Contracts.Base;
using Spring.Data.NHibernate.Support;
using Spring.Transaction.Interceptor;
using NHibernate;
using NHibernate.Criterion;

namespace Acaweb.Data.Dao.NHibernate
{
    /// <summary>
    /// Base class for data access operations.
    /// </summary>
    public class HibernateDao<T, TId> : Spring.Data.NHibernate.Support.HibernateDaoSupport
    {
        #region IDao<T,TId> Members

        public void GetConfiguration()
        {

        }

        [Transaction(ReadOnly = true)]
        public IList<T> GetAll<T>() where T : class
        {
            ICriteria criteria = Session.CreateCriteria<T>();
            return criteria.List<T>();
        }

        [Transaction(ReadOnly = false)]
        public void Save(T obj)
        {
            HibernateTemplate.Save(obj);
        }

        [Transaction]
        public void Update(T obj)
        {
            HibernateTemplate.Update(obj);
        }

        [Transaction]
        public void Delete(T obj)
        {
            HibernateTemplate.Delete(obj);
        }

        [Transaction(ReadOnly = true)]
        public T GetById(TId Id)
        {
            return Session.Get<T>(Id);
        }

        [Transaction(ReadOnly = true)]
        public IList<T> GetList(DetachedCriteria dq)
        {
            return dq.GetExecutableCriteria(Session).List<T>();
        }

        [Transaction(ReadOnly = true)]
        public IList<TType> GetList<TType>(DetachedCriteria dq)
        {
            return dq.GetExecutableCriteria(Session).List<TType>();
        }

        #endregion
    }
}